package com.igeekhome.ccs.controller;

import com.igeekhome.ccs.biz.IAnnouncementBiz;
import com.igeekhome.ccs.biz.ICustomerServiceBiz;
import com.igeekhome.ccs.entity.Announcement;
import com.igeekhome.ccs.entity.AnnouncementJoinCs;
import com.igeekhome.ccs.entity.CustomerService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/announcement")
public class AnnouncementController {

    @Autowired
    private IAnnouncementBiz biz;
    @Autowired
    private ICustomerServiceBiz csbiz;

    /**
     * 设置/通知公告
     * 新增一条公告，前端需要字段:title,content
     * 已测试
     * @param Announcement对象ad
     * @return
     */
    @PostMapping("/add")
    @ResponseBody
    public int save(@RequestBody Map<String, String> map) {

        Announcement ad = new Announcement();
        ad.setTitle(map.get("title"));
        ad.setContent(map.get("content"));

        CustomerService cs = csbiz.getOneByUsername((String) SecurityUtils.getSubject().getPrincipal());
        ad.setCreateId(cs.getCsId());

        Date date = new Date();
        ad.setCreateTime(date);
        return biz.save(ad);
    }


    /**
     * 设置/通知公告
     * 更新一条公告，前端需要字段:title,content,annoucementId
     * 已测试
     * @param Announcement对象ad
     * @return
     */
    @PostMapping("/update")
    @ResponseBody
    public int update(@RequestBody Announcement ad){
        return biz.update(ad);
    }


    /**
     * 设置/通知公告
     * 根据title查询公告，前端需要字段:title
     * 已测试
     * @param 标题title
     * @return
     */
    @GetMapping("/get/title")
    @ResponseBody
    public List<AnnouncementJoinCs> queryByTitle(@RequestBody Map<String, String> map){
        List<AnnouncementJoinCs> list = biz.queryByTitle(map.get("title"));
        if(list.size()!=0){
            return list;
        }else{
            System.out.println("标题查询为空");
        }
        return list;
    }

    /**
     * 设置/通知公告
     * 根据起止时间查询公告，前端需要字段:start, end
     * 已测试
     * @param 起止时间
     * @return
     */
    @GetMapping("/get/time")
    @ResponseBody
    public List<AnnouncementJoinCs> queryByTime(@RequestBody Map<String, String> map){

        //需要把前端的字符串类型时间转化为Date类型

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date startTime = null;
        Date endTime = null;
        List<AnnouncementJoinCs> list = null;
        try {
            startTime = simpleDateFormat.parse(map.get("start"));
            endTime = simpleDateFormat.parse(map.get("end"));
            list = biz.queryByTime(startTime, endTime);
            if(list.size()!=0){
                return list;
            }else{
                System.out.println("起止时间查询为空");
            }

        } catch (ParseException e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 设置/通知公告
     * 查询所有公告
     * 已测试
     * @param
     * @return
     */

    @GetMapping("/get/all")
    @ResponseBody
    public List<AnnouncementJoinCs> getAll(){
        List<AnnouncementJoinCs> list = biz.getAll();
        if(list.size()==0){
            System.out.println("无公告");
        }
        return list;
    }
    /**
     * 设置/通知公告
     * 查询单条公告，需要获得前端选中的那条的id
     * 已测试
     * @param
     * @return
     */
    @DeleteMapping("/delete")
    @ResponseBody
    public int delete(@RequestBody Map<String, String> map){
        int id = Integer.parseInt(map.get("announcementId"));
        int a =biz.delete(id);
        //queryById
        System.out.println();
        return a;
    }
    /**
     * 设置/通知公告
     * 批量删除公告，需要获得前端选中的多条的ids
     * 已测试
     * @param
     * @return
     */
    @DeleteMapping("/deleteBatch")
    @ResponseBody
    public int delete(@RequestBody List<Integer> ids){
            int a =biz.deleteBatch(ids);
            return a;
    }
}
